<?php

namespace app\index\controller;
use think\Db;
use app\index\controller\CommonController;
use app\index\model\Question;

/*
 * 贷款问答
 */
class QuestionController extends CommonController
{
    /*
     * 贷款问答
     */
    public function index()
    {
        $param = input('param.');

        if(isset($param['is_hot'])){
            $where['is_hot'] = $param['is_hot'];
        }else{
            $status = isset($param['status']) ? $param['status'] : 1;
            $where['status'] = $status;
        }
        $questionList = Question::with('answer')
            ->where($where)
            ->field('id, user_id, question, description, add_time')
            ->order('add_time desc')
            ->paginate(8);

        if (count($questionList) > 0) {
            foreach ($questionList as $key => $value) {
                $questionList[$key]['count'] = Question::getAnswerCount($value['id']);
            }
        }
        $questiontype0 = Question::field('id, question')
            ->where('questiontype','=',0)
            ->select();
        $questiontype1 = Question::field('id, question')
            ->where('questiontype','=',1)
            ->select();
        $questiontype2 = Question::field('id, question')
            ->where('questiontype','=',2)
            ->select();
        $questiontype3 = Question::field('id, question')
            ->where('questiontype','=',3)
            ->select();

        return view('index', [
            'menuIndex' => 6,
            'questionList' => $questionList,
            'questiontype0' => $questiontype0,
            'questiontype1' => $questiontype1,
            'questiontype2' => $questiontype2,
            'questiontype3' => $questiontype3,
        ]);
    }

    /*
     * 问题详情
     */
    public function content()
    {
        $id = input('param.id');
/*        $questionList = Question::with('answer')
            ->where('status','=', $id);
            ->select();*/
        $questions = Db::query('select * from da_question a,da_admin b where a.user_id=b.id and a.user_id=?',[$id]);
        $questionList = Db::query('select * from da_question a,da_answer b,da_city c where a.id=b.question_id and b.city_id=c.id and a.id=?',[$id]);
        $questiontype0 = Question::field('id, question')
            ->where('questiontype','=',0)
            ->select();
        $questiontype1 = Question::field('id, question')
            ->where('questiontype','=',1)
            ->select();
        $questiontype2 = Question::field('id, question')
            ->where('questiontype','=',2)
            ->select();
        $questiontype3 = Question::field('id, question')
            ->where('questiontype','=',3)
            ->select();
        return view('content',[
            'menuIndex' => 6,
            'questionList' => $questionList,
            'questions' => $questions,
            'questiontype0' => $questiontype0,
            'questiontype1' => $questiontype1,
            'questiontype2' => $questiontype2,
            'questiontype3' => $questiontype3,
        ]);
    }

}